
const model = require('../schems')


let commentsAction={}

commentsAction.getCmts= function(id){
  return new Promise(function(resolve,reject){
    model.comment.find({'artid':id,'isOK':true}).sort({'time':-1}).exec(function(err,doc){
      if(doc){
        resolve(JSON.parse(JSON.stringify(doc)))
      }
    })
  })
}

//一般回复
commentsAction.setCmts = function(obj){
  return new Promise(function(resolve,reject){
    let comment = new model.comment(obj)
    comment.save().then(e=>{
      let doc = JSON.parse(JSON.stringify(e))
      resolve(doc)
    })
  })
}

//引用回复
commentsAction.reCmts = function(_id,obj){
  return new Promise(function(resolve,reject){
    model.comment.findByIdAndUpdate(_id,{$push:{'replyBody':obj}},{ new: true }).exec(function(err,doc){
      if(doc){
        resolve(doc)
      }
    })
  })
}

//所有评论
commentsAction.all = function(callback){
  model.comment.find({}).populate('artid','title').sort({time:-1}).exec(function(err,doc){
    if(err) return;
    callback(JSON.parse(JSON.stringify(doc)))
  })
}

//删除评论
commentsAction.deById = function(ids,callback){
  var idArr= ids.split(',') 
  model.comment.deleteMany({'_id':{$in:idArr}},function(err,doc){
    if(err){
      console.log(err)
      return
    }
    console.log(doc)
    callback(doc)
  })
}
//审核评论
commentsAction.pastById = function(ids,ok,callback){
  var idArr= ids.split(',') 
  console.log(idArr)
  model.comment.updateMany({_id:{$in:idArr}},{'isOK':ok},function(err,doc){
    if(err){
      return '更新失败'
    }
    callback(doc)
  })
}

//获取单条评论
commentsAction.getOne = function(_id,callback){
  model.comment.findById(_id,function(err,doc){
    if(err){
      return
    }
    callback(JSON.parse(JSON.stringify(doc)))
  })
}




module.exports = commentsAction